<?php
/**
 * Created by PhpStorm.
 * User: wanglei
 * Date: 2016/7/30
 * Time: 14:52
 * 支付
 */
defined('IN_PHPCMS') or exit('No permission resources.');
//模型缓存路径
define('CACHE_MODEL_PATH',CACHE_PATH.'caches_model'.DIRECTORY_SEPARATOR.'caches_data'.DIRECTORY_SEPARATOR);
pc_base::load_app_func('util','content');
pc_base::load_sys_class('form', '', 0);
class payment{
    public function __construct()
    {
        pc_base::load_sys_class('alipay_submit','libs/classes/alipay',0);

    }

    /**
     * 调用支付宝
     */
    public function pay()
    {
        //支付金额
        $pay_money = $_POST['rechargeMoney'];
        $user_id= param::get_cookie('_userid',0);
        $username= param::get_cookie('_username','');
        if(!$user_id){
            header("location:/index.php?m=shuafen&c=web&a=login");exit();
        }
        //通过支付金额获取充值金额
        $shuafen_recharge_conf = pc_base::load_model('shuafen_recharge_conf_model');
        $recharge_conf = $shuafen_recharge_conf->get_one(array('is_show'=>'Y','pay_money'=>$pay_money));
        if(!$recharge_conf) exit('错误的充值金额,请重新登录!');
        //充值金额
        $money = $recharge_conf['money'];

        $alipay_config = pc_base::load_config('alipay_config');

        //商户订单号，商户网站订单系统中唯一订单号，必填
        $time = time();
        $out_trade_no = $user_id.'_'.$time;

        //订单名称，必填
        $subject = '账户充值';;

        //付款金额，必填
        $total_fee =floatval($pay_money);

        //收银台页面上，商品展示的超链接，必填
        $show_url = '';

        //商品描述，可空
        $body = $username.':账号充值';


        //构造要请求的参数数组
        $parameter = array(
            "service"       => $alipay_config['service'],
            "partner"       => $alipay_config['partner'],
            "seller_id"  => $alipay_config['seller_id'],
            "payment_type"	=> $alipay_config['payment_type'],
            "notify_url"	=> $alipay_config['notify_url'],
            "return_url"	=> $alipay_config['return_url'],
            "_input_charset"	=> trim(strtolower($alipay_config['input_charset'])),
            "out_trade_no"	=> $out_trade_no,
            "subject"	=> $subject,
            "total_fee"	=> $total_fee,
            "show_url"	=> $show_url,
            "app_pay"	=> "Y",//启用此参数能唤起钱包APP支付宝
            "body"	=> $body,


        );
        //记录下单日志
        $log = $this->payLog($out_trade_no,$user_id,$username,$pay_money,$money);
        if(!$log){
            header("location:/index.php?m=shuafen&c=web&a=login");
        }
        //建立请求
        $alipaySubmit = new alipay_submit($alipay_config);
        $html_text = $alipaySubmit->buildRequestForm($parameter,"get", "确认");
        echo $html_text;
    }
    /**
     *记录下单日志
     */
    public function payLog($out_trade_no,$user_id,$username,$pay_money,$money)
    {
        $data = array(
            'out_trade_no'=>$out_trade_no,
            'member_id'=>$user_id,
            'member_tel'=>$username,
            'pay_money'=>$pay_money,
            'money'=>$money,
            'recharge_type'=>'支付宝',
            'create_time'=>time(),
            'trade_status'=>'unpaid'
        );
        $model = pc_base::load_model('shuafen_recharge_record_model');
        return $model->insert($data);
    }
    public function test(){
        $common = pc_base::load_app_class('common');
        $common->payCallback('4_1471012122','1111');

    }

}